using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;

namespace Adex
{
	/// <summary>
	/// Strongly-typed collection for the Country class.
	/// </summary>
	[Serializable]
	public partial class CountryCollection : ActiveList<Country, CountryCollection> 
	{	   
		public CountryCollection() {}

	}

	/// <summary>
	/// This is an ActiveRecord class which wraps the Country table.
	/// </summary>
	[Serializable]
	public partial class Country : ActiveRecord<Country>
	{
		#region .ctors and Default Settings
		
		public Country()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}

		
		private void InitSetDefaults() { SetDefaults(); }

		
		public Country(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}

		public Country(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}

		 
		public Country(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}

		
		protected static void SetSQLProps() { GetTableSchema(); }

		
		#endregion
		
		#region Schema and Query Accessor
		public static Query CreateQuery() { return new Query(Schema); }

		
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}

		}

		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("Country", TableType.Table, DataService.GetInstance("Adex"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarCountryID = new TableSchema.TableColumn(schema);
				colvarCountryID.ColumnName = "CountryID";
				colvarCountryID.DataType = DbType.Byte;
				colvarCountryID.MaxLength = 0;
				colvarCountryID.AutoIncrement = true;
				colvarCountryID.IsNullable = false;
				colvarCountryID.IsPrimaryKey = true;
				colvarCountryID.IsForeignKey = false;
				colvarCountryID.IsReadOnly = false;
				colvarCountryID.DefaultSetting = @"";
				colvarCountryID.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCountryID);
				
				TableSchema.TableColumn colvarTld = new TableSchema.TableColumn(schema);
				colvarTld.ColumnName = "TLD";
				colvarTld.DataType = DbType.String;
				colvarTld.MaxLength = 50;
				colvarTld.AutoIncrement = false;
				colvarTld.IsNullable = true;
				colvarTld.IsPrimaryKey = false;
				colvarTld.IsForeignKey = false;
				colvarTld.IsReadOnly = false;
				colvarTld.DefaultSetting = @"";
				colvarTld.ForeignKeyTableName = "";
				schema.Columns.Add(colvarTld);
				
				TableSchema.TableColumn colvarCountryName = new TableSchema.TableColumn(schema);
				colvarCountryName.ColumnName = "CountryName";
				colvarCountryName.DataType = DbType.String;
				colvarCountryName.MaxLength = 50;
				colvarCountryName.AutoIncrement = false;
				colvarCountryName.IsNullable = true;
				colvarCountryName.IsPrimaryKey = false;
				colvarCountryName.IsForeignKey = false;
				colvarCountryName.IsReadOnly = false;
				colvarCountryName.DefaultSetting = @"";
				colvarCountryName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCountryName);
				
				TableSchema.TableColumn colvarFIPS104 = new TableSchema.TableColumn(schema);
				colvarFIPS104.ColumnName = "FIPS104";
				colvarFIPS104.DataType = DbType.String;
				colvarFIPS104.MaxLength = 50;
				colvarFIPS104.AutoIncrement = false;
				colvarFIPS104.IsNullable = true;
				colvarFIPS104.IsPrimaryKey = false;
				colvarFIPS104.IsForeignKey = false;
				colvarFIPS104.IsReadOnly = false;
				colvarFIPS104.DefaultSetting = @"";
				colvarFIPS104.ForeignKeyTableName = "";
				schema.Columns.Add(colvarFIPS104);
				
				TableSchema.TableColumn colvarISO2 = new TableSchema.TableColumn(schema);
				colvarISO2.ColumnName = "ISO2";
				colvarISO2.DataType = DbType.String;
				colvarISO2.MaxLength = 50;
				colvarISO2.AutoIncrement = false;
				colvarISO2.IsNullable = true;
				colvarISO2.IsPrimaryKey = false;
				colvarISO2.IsForeignKey = false;
				colvarISO2.IsReadOnly = false;
				colvarISO2.DefaultSetting = @"";
				colvarISO2.ForeignKeyTableName = "";
				schema.Columns.Add(colvarISO2);
				
				TableSchema.TableColumn colvarISO3 = new TableSchema.TableColumn(schema);
				colvarISO3.ColumnName = "ISO3";
				colvarISO3.DataType = DbType.String;
				colvarISO3.MaxLength = 50;
				colvarISO3.AutoIncrement = false;
				colvarISO3.IsNullable = true;
				colvarISO3.IsPrimaryKey = false;
				colvarISO3.IsForeignKey = false;
				colvarISO3.IsReadOnly = false;
				colvarISO3.DefaultSetting = @"";
				colvarISO3.ForeignKeyTableName = "";
				schema.Columns.Add(colvarISO3);
				
				TableSchema.TableColumn colvarISONo = new TableSchema.TableColumn(schema);
				colvarISONo.ColumnName = "ISONo";
				colvarISONo.DataType = DbType.String;
				colvarISONo.MaxLength = 50;
				colvarISONo.AutoIncrement = false;
				colvarISONo.IsNullable = true;
				colvarISONo.IsPrimaryKey = false;
				colvarISONo.IsForeignKey = false;
				colvarISONo.IsReadOnly = false;
				colvarISONo.DefaultSetting = @"";
				colvarISONo.ForeignKeyTableName = "";
				schema.Columns.Add(colvarISONo);
				
				TableSchema.TableColumn colvarCapital = new TableSchema.TableColumn(schema);
				colvarCapital.ColumnName = "Capital";
				colvarCapital.DataType = DbType.String;
				colvarCapital.MaxLength = 50;
				colvarCapital.AutoIncrement = false;
				colvarCapital.IsNullable = true;
				colvarCapital.IsPrimaryKey = false;
				colvarCapital.IsForeignKey = false;
				colvarCapital.IsReadOnly = false;
				colvarCapital.DefaultSetting = @"";
				colvarCapital.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCapital);
				
				TableSchema.TableColumn colvarRegions = new TableSchema.TableColumn(schema);
				colvarRegions.ColumnName = "Regions";
				colvarRegions.DataType = DbType.String;
				colvarRegions.MaxLength = 50;
				colvarRegions.AutoIncrement = false;
				colvarRegions.IsNullable = true;
				colvarRegions.IsPrimaryKey = false;
				colvarRegions.IsForeignKey = false;
				colvarRegions.IsReadOnly = false;
				colvarRegions.DefaultSetting = @"";
				colvarRegions.ForeignKeyTableName = "";
				schema.Columns.Add(colvarRegions);
				
				TableSchema.TableColumn colvarCurrency = new TableSchema.TableColumn(schema);
				colvarCurrency.ColumnName = "Currency";
				colvarCurrency.DataType = DbType.String;
				colvarCurrency.MaxLength = 50;
				colvarCurrency.AutoIncrement = false;
				colvarCurrency.IsNullable = true;
				colvarCurrency.IsPrimaryKey = false;
				colvarCurrency.IsForeignKey = false;
				colvarCurrency.IsReadOnly = false;
				colvarCurrency.DefaultSetting = @"";
				colvarCurrency.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCurrency);
				
				TableSchema.TableColumn colvarCurrencyCode = new TableSchema.TableColumn(schema);
				colvarCurrencyCode.ColumnName = "CurrencyCode";
				colvarCurrencyCode.DataType = DbType.String;
				colvarCurrencyCode.MaxLength = 50;
				colvarCurrencyCode.AutoIncrement = false;
				colvarCurrencyCode.IsNullable = true;
				colvarCurrencyCode.IsPrimaryKey = false;
				colvarCurrencyCode.IsForeignKey = false;
				colvarCurrencyCode.IsReadOnly = false;
				colvarCurrencyCode.DefaultSetting = @"";
				colvarCurrencyCode.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCurrencyCode);
				
				TableSchema.TableColumn colvarPopulation = new TableSchema.TableColumn(schema);
				colvarPopulation.ColumnName = "Population";
				colvarPopulation.DataType = DbType.String;
				colvarPopulation.MaxLength = 50;
				colvarPopulation.AutoIncrement = false;
				colvarPopulation.IsNullable = true;
				colvarPopulation.IsPrimaryKey = false;
				colvarPopulation.IsForeignKey = false;
				colvarPopulation.IsReadOnly = false;
				colvarPopulation.DefaultSetting = @"";
				colvarPopulation.ForeignKeyTableName = "";
				schema.Columns.Add(colvarPopulation);
				
				TableSchema.TableColumn colvarActive = new TableSchema.TableColumn(schema);
				colvarActive.ColumnName = "Active";
				colvarActive.DataType = DbType.Boolean;
				colvarActive.MaxLength = 0;
				colvarActive.AutoIncrement = false;
				colvarActive.IsNullable = false;
				colvarActive.IsPrimaryKey = false;
				colvarActive.IsForeignKey = false;
				colvarActive.IsReadOnly = false;
				
						colvarActive.DefaultSetting = @"((0))";
				colvarActive.ForeignKeyTableName = "";
				schema.Columns.Add(colvarActive);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["Adex"].AddSchema("Country",schema);
			}

		}

		#endregion
		
		#region Props
		
		  
		[XmlAttribute("CountryID")]
		public byte CountryID 
		{
			get { return GetColumnValue<byte>("CountryID"); }

			set { SetColumnValue("CountryID", value); }

		}

		  
		[XmlAttribute("Tld")]
		public string Tld 
		{
			get { return GetColumnValue<string>("TLD"); }

			set { SetColumnValue("TLD", value); }

		}

		  
		[XmlAttribute("CountryName")]
		public string CountryName 
		{
			get { return GetColumnValue<string>("CountryName"); }

			set { SetColumnValue("CountryName", value); }

		}

		  
		[XmlAttribute("FIPS104")]
		public string FIPS104 
		{
			get { return GetColumnValue<string>("FIPS104"); }

			set { SetColumnValue("FIPS104", value); }

		}

		  
		[XmlAttribute("ISO2")]
		public string ISO2 
		{
			get { return GetColumnValue<string>("ISO2"); }

			set { SetColumnValue("ISO2", value); }

		}

		  
		[XmlAttribute("ISO3")]
		public string ISO3 
		{
			get { return GetColumnValue<string>("ISO3"); }

			set { SetColumnValue("ISO3", value); }

		}

		  
		[XmlAttribute("ISONo")]
		public string ISONo 
		{
			get { return GetColumnValue<string>("ISONo"); }

			set { SetColumnValue("ISONo", value); }

		}

		  
		[XmlAttribute("Capital")]
		public string Capital 
		{
			get { return GetColumnValue<string>("Capital"); }

			set { SetColumnValue("Capital", value); }

		}

		  
		[XmlAttribute("Regions")]
		public string Regions 
		{
			get { return GetColumnValue<string>("Regions"); }

			set { SetColumnValue("Regions", value); }

		}

		  
		[XmlAttribute("Currency")]
		public string Currency 
		{
			get { return GetColumnValue<string>("Currency"); }

			set { SetColumnValue("Currency", value); }

		}

		  
		[XmlAttribute("CurrencyCode")]
		public string CurrencyCode 
		{
			get { return GetColumnValue<string>("CurrencyCode"); }

			set { SetColumnValue("CurrencyCode", value); }

		}

		  
		[XmlAttribute("Population")]
		public string Population 
		{
			get { return GetColumnValue<string>("Population"); }

			set { SetColumnValue("Population", value); }

		}

		  
		[XmlAttribute("Active")]
		public bool Active 
		{
			get { return GetColumnValue<bool>("Active"); }

			set { SetColumnValue("Active", value); }

		}

		
		#endregion
		
		
		#region PrimaryKey Methods
		
		public Adex.CommercialLogCollection CommercialLogRecords()
		{
			return new Adex.CommercialLogCollection().Where(CommercialLog.Columns.CountryID, CountryID).Load();
		}

		public Adex.CountryMediumCollection CountryMedia()
		{
			return new Adex.CountryMediumCollection().Where(CountryMedium.Columns.CountryID, CountryID).Load();
		}

		public Adex.InternetLogCollection InternetLogRecords()
		{
			return new Adex.InternetLogCollection().Where(InternetLog.Columns.CountryID, CountryID).Load();
		}

		public Adex.OutdoorLogCollection OutdoorLogRecords()
		{
			return new Adex.OutdoorLogCollection().Where(OutdoorLog.Columns.CountryID, CountryID).Load();
		}

		public Adex.PressLogCollection PressLogRecords()
		{
			return new Adex.PressLogCollection().Where(PressLog.Columns.CountryID, CountryID).Load();
		}

		public Adex.SiteUserCollection SiteUserRecords()
		{
			return new Adex.SiteUserCollection().Where(SiteUser.Columns.CountryID, CountryID).Load();
		}

		#endregion
		
			
		
		//no foreign key tables defined (0)
		
		
		
		#region Many To Many Helpers
		
		 
		public Adex.MediaTypeCollection GetMediaTypeCollection() { return Country.GetMediaTypeCollection(this.CountryID); }

		public static Adex.MediaTypeCollection GetMediaTypeCollection(byte varCountryID)
		{
			SubSonic.QueryCommand cmd = new SubSonic.QueryCommand(
				"SELECT * FROM MediaType INNER JOIN CountryMedia ON "+
				"MediaType.MediaTypeCode=CountryMedia.MediaTypeCode WHERE CountryMedia.CountryID=@CountryID", Country.Schema.Provider.Name);
			
			cmd.AddParameter("@CountryID", varCountryID, DbType.Byte);
			IDataReader rdr = SubSonic.DataService.GetReader(cmd);
			MediaTypeCollection coll = new MediaTypeCollection();
			coll.LoadAndCloseReader(rdr);
			return coll;
		}

		
		public static void SaveMediaTypeMap(byte varCountryID, MediaTypeCollection items)
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			QueryCommand cmdDel = new QueryCommand("DELETE FROM CountryMedia WHERE CountryID=@CountryID", Country.Schema.Provider.Name);
			cmdDel.AddParameter("@CountryID", varCountryID);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (MediaType item in items)
			{
				CountryMedium varCountryMedium = new CountryMedium();
				varCountryMedium.SetColumnValue("CountryID", varCountryID);
				varCountryMedium.SetColumnValue("MediaTypeCode", item.GetPrimaryKeyValue());
				varCountryMedium.Save();
			}

		}

		public static void SaveMediaTypeMap(byte varCountryID, System.Web.UI.WebControls.ListItemCollection itemList) 
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			 QueryCommand cmdDel = new QueryCommand("DELETE FROM CountryMedia WHERE CountryID=@CountryID", Country.Schema.Provider.Name);
			cmdDel.AddParameter("@CountryID", varCountryID);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (System.Web.UI.WebControls.ListItem l in itemList) 
			{
				if (l.Selected) 
				{
					CountryMedium varCountryMedium = new CountryMedium();
					varCountryMedium.SetColumnValue("CountryID", varCountryID);
					varCountryMedium.SetColumnValue("MediaTypeCode", l.Value);
					varCountryMedium.Save();
				}

			}

		}

		public static void SaveMediaTypeMap(byte varCountryID , byte[] itemList) 
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			 QueryCommand cmdDel = new QueryCommand("DELETE FROM CountryMedia WHERE CountryID=@CountryID", Country.Schema.Provider.Name);
			cmdDel.AddParameter("@CountryID", varCountryID);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (byte item in itemList) 
			{
				CountryMedium varCountryMedium = new CountryMedium();
				varCountryMedium.SetColumnValue("CountryID", varCountryID);
				varCountryMedium.SetColumnValue("MediaTypeCode", item);
				varCountryMedium.Save();
			}

		}

		
		public static void DeleteMediaTypeMap(byte varCountryID) 
		{
			QueryCommand cmdDel = new QueryCommand("DELETE FROM CountryMedia WHERE CountryID=@CountryID", Country.Schema.Provider.Name);
			cmdDel.AddParameter("@CountryID", varCountryID);
			DataService.ExecuteQuery(cmdDel);
		}

		
		#endregion
		
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(string varTld,string varCountryName,string varFIPS104,string varISO2,string varISO3,string varISONo,string varCapital,string varRegions,string varCurrency,string varCurrencyCode,string varPopulation,bool varActive)
		{
			Country item = new Country();
			
			item.Tld = varTld;
			
			item.CountryName = varCountryName;
			
			item.FIPS104 = varFIPS104;
			
			item.ISO2 = varISO2;
			
			item.ISO3 = varISO3;
			
			item.ISONo = varISONo;
			
			item.Capital = varCapital;
			
			item.Regions = varRegions;
			
			item.Currency = varCurrency;
			
			item.CurrencyCode = varCurrencyCode;
			
			item.Population = varPopulation;
			
			item.Active = varActive;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(byte varCountryID,string varTld,string varCountryName,string varFIPS104,string varISO2,string varISO3,string varISONo,string varCapital,string varRegions,string varCurrency,string varCurrencyCode,string varPopulation,bool varActive)
		{
			Country item = new Country();
			
				item.CountryID = varCountryID;
				
				item.Tld = varTld;
				
				item.CountryName = varCountryName;
				
				item.FIPS104 = varFIPS104;
				
				item.ISO2 = varISO2;
				
				item.ISO3 = varISO3;
				
				item.ISONo = varISONo;
				
				item.Capital = varCapital;
				
				item.Regions = varRegions;
				
				item.Currency = varCurrency;
				
				item.CurrencyCode = varCurrencyCode;
				
				item.Population = varPopulation;
				
				item.Active = varActive;
				
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		#endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string CountryID = @"CountryID";
			 public static string Tld = @"TLD";
			 public static string CountryName = @"CountryName";
			 public static string FIPS104 = @"FIPS104";
			 public static string ISO2 = @"ISO2";
			 public static string ISO3 = @"ISO3";
			 public static string ISONo = @"ISONo";
			 public static string Capital = @"Capital";
			 public static string Regions = @"Regions";
			 public static string Currency = @"Currency";
			 public static string CurrencyCode = @"CurrencyCode";
			 public static string Population = @"Population";
			 public static string Active = @"Active";
						
		}

		#endregion
	}

}

